package com.eco
{
	import com.eco.*;
	import com.framework.*;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	import flash.utils.*;

	//卫士类，继承自基本物体
	public class Fighter extends RigidBall
	{
		//测试测试测试测试测试测试测试测试测试测试测试
		private var test:MovieClip;
		//测试测试测试测试测试测试测试测试测试测试测试
		
		//卫士在游戏中的属性
		private var nLife:int;//卫士的生命值
		private var nAcceleration:Number;//加速度
		private var iMaxSpeed:int;//移动的最大速度
		private var iDirLeft:uint;//表示卫士受键盘控制的移动方向，1为移动，0为停止
		private var iDirRight:uint;//通过按键的组合以及同轴方向数值的抵消获得最终移动方向
		private var iDirUp:uint;
		private var iDirDown:uint;
		public var iSensitivity:int;//敏感度，表示卫士跟随鼠标运动时的灵敏程度，越小越灵敏
		public var iSuperArmor:int;//霸体时间，用于缩短硬直状态持续时间
		private var iFreezedFrame:int;//当前硬直持续时间
		private var iRecoverFrames:int; //从硬直中恢复所需要的时间
		public var iNormalMass:int;//原始质量
		public var iNormalSensitivity:int;//原始敏感度
		public var nNormalAcceleration:Number;//原始加速度
		public var iNormalMaxSpeed:int;//原始最大速度
		public var nFrictionRate:Number;
		
		//卫士可以释放旋风技能，进入旋风状态时，能量（nEnegy）会增强
		private var nStrengthenRate:Number;//旋风时能量增强的比率
		public var iColdDownFrames:int;//旋风技能冷却时间，也是最长持续时间
		public var iLastFrame:int;//旋风技能已经持续时间
		public var bInvincible:Boolean;//表示卫士是否处于无敌状态，自身不受影响而可以影响别的物体，true为是
		public var bRage:Boolean;//表示卫士是否处于暴怒状态，在旋风状态下碰撞敌人能直接杀死敌人，true为是
		
		public var iState:int;//卫士的状态
		public var iAnimationTag:int;//动画的状态标签
		
		//以下声明各种状态
		static public const STATE_WIND_START:int = 1;//旋风开始并且进行中
		static public const STATE_READY:int = 0;//处于冷却并就绪的状态
		static public const STATE_WIND_CD:int = 2;//进入旋风的强制冷却时间
		static public const STATE_FREEZED:int = 3;//硬直
		static public const STATE_DEAD:int = 4;
		static public const STATE_END:int = 5;
				
		public function Fighter( mass:Number = 1, r:int = 1, sensit:int = 5, life:int = 1, cdtime:int = 12, strengthen:Number = 1 )
		{
			//测试测试测试测试测试测试测试测试测试测试测试
			test =  new resFighter();
			test.scaleX = 0.8;
			test.scaleY = 0.8;
			test.gotoAndStop("IDLE");
			addChild(test);
			//测试测试测试测试测试测试测试测试测试测试测试
			
			//基类构造函数，建立刚体
			super(mass,r);
			
			//卫士在游戏中的属性	
			iNormalMass = mass;
			iNormalSensitivity = sensit;
			nNormalAcceleration = 1;
			iNormalMaxSpeed = 8;
			nLife = life;//生命值
			iSensitivity = sensit;//敏感度
			nAcceleration = nNormalAcceleration;
			iMaxSpeed = iNormalMaxSpeed;
			nFrictionRate = 0.9;
			
			
			iSuperArmor = 0;//霸体
			iRecoverFrames = 0;
			iColdDownFrames = cdtime;
			iLastFrame = 0;
			nStrengthenRate = strengthen;//能量增强率
			
			bInvincible = false;
			bRage = false;
			
			iState = STATE_READY;
			
			//添加初始化函数与更新函数的触发事件侦听
			addEventListener(Event.ADDED_TO_STAGE, Initiate);
			
		}
		
		private function Initiate(e:Event):void
		{
			//添加两个鼠标事件，分别对应旋风技能的开始与结束
			//stage.addEventListener(MouseEvent.MOUSE_DOWN,StartWhirlwind);
			//stage.addEventListener(MouseEvent.MOUSE_UP,EndWhirlwind);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
			
			this.removeEventListener(Event.ADDED_TO_STAGE, Initiate);
		}
		
		protected function CreateSoundEvent(type:String, name:String ):void
		{
			var evt:CustomEventSound = new CustomEventSound( type, name );
			dispatchEvent(evt);
		}
		
		private function KeyDown(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case Keyboard.UP:
				{
					iDirUp = 1;
					break;
				}
				case Keyboard.DOWN:
				{
					iDirDown = 1;
					break;
				}
				case Keyboard.LEFT:
				{
					iDirLeft = 1;
					break;
				}
				case Keyboard.RIGHT:
				{
					iDirRight = 1;
					break;
				}
				case Keyboard.SPACE:
				{
					StartWhirlwind();
					break;
				}
			}
		}
		
		private function KeyUp(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case Keyboard.UP:
				{
					iDirUp = 0;
					break;
				}
				case Keyboard.DOWN:
				{
					iDirDown = 0;
					break;
				}
				case Keyboard.LEFT:
				{
					iDirLeft = 0;
					break;
				}
				case Keyboard.RIGHT:
				{
					iDirRight = 0;
					break;
				}
				case Keyboard.SPACE:
				{
					EndWhirlwind();
					break;
				}
			}
		}
		
		private function StartWhirlwind(e:MouseEvent=null):void
		{
			if( iState != STATE_READY )
				return;
			else
			{
				test.gotoAndPlay("WIND");
				//CreateSoundEvent( CustomEventSound.PLAY_SOUND, SoundInfo.BGM_MAINMENU);
			
				//v2dSpeed.scale(3);
				nAcceleration = 3*nNormalAcceleration;
				iMaxSpeed = 3*iNormalMaxSpeed;
				iState = STATE_WIND_START;
			}
				
			//因此，强制冷却和硬直时将无法通过鼠标事件转换到就绪或进行状态
		}
		
		private function EndWhirlwind(e:MouseEvent=null):void
		{
			//把处于进行中的技能中断，并进入正常冷却与就绪状态
			if( iState == STATE_WIND_START )
			{
				nAcceleration = nNormalAcceleration;
				iMaxSpeed = iNormalMaxSpeed;
				iState = STATE_READY;
			}
			//因此，强制冷却时将无法通过键盘事件转换到就绪或进行状态
		}
		
		/*private function FollowMouse():void
		{
			//根据鼠标位置得到当前速度
			//如果敏感度为0，则不跟随鼠标
			if( iSensitivity == 0 )
				return;
			else
			{
				v2dSpeed.x = (stage.mouseX - this.parent.x - this.x)/iSensitivity;
				v2dSpeed.y = (stage.mouseY - this.parent.y - this.y)/iSensitivity;
			}
		}*/
		
		private function MoveCtrlKeyboard():void
		{
			v2dSpeed.x += nAcceleration * (iDirRight - iDirLeft);
			v2dSpeed.y += nAcceleration * (iDirDown - iDirUp);
			if( v2dSpeed.getLength()>iMaxSpeed )
			{
				v2dSpeed.setLength( iMaxSpeed );
			}
		}
		
		override protected function Move():void
		{
			super.Move();
		}
		
		override protected function Rotate():void
		{
			super.Rotate();
		}
		private function UpdateFreezingState():void
		{
			iFreezedFrame++;
			if( iFreezedFrame >= (iRecoverFrames - iSuperArmor) )
			{
				iFreezedFrame = 0;
				iState = STATE_READY;
				test.gotoAndStop("IDLE");
			}
		}
		
		public function LoseLife(loselife:int):void
		{
			//减少生命
		}
		
		public function Freezed(freezedtime:int ):void
		{
			//进入硬直状态，或者刷新硬直持续时间
			test.gotoAndPlay("FREEZED");
			//CreateSoundEvent( CustomEventSound.PLAY_SOUND, SoundInfo.EFF_FIGHTER_FREEZED);
			iState = STATE_FREEZED;
			if( iFreezedFrame == 0 )
			{
				//如果硬直已持续时间为0，说明之前不处于硬直恢复状态，刷新硬直时间为外部给与的时间
				iRecoverFrames = freezedtime;//
			}
			else
			{
				//如果硬直已持续了一段时间，说明当前正处于硬直的恢复期，只把给定值的1/5硬直时间加到总恢复时间上
				iRecoverFrames += freezedtime/5;
			}
			
		}
		
		public function Update(e:Event = null):void
		{
			Move();
			Rotate();
			
			switch( iState )
			{
				case STATE_WIND_START:
				{
					//处于进行状态的旋风
					
					MoveCtrlKeyboard();
					//Friction();
					iLastFrame ++;//持续时间递增
					iMass = 2*iNormalMass;//质量增大
					nAcceleration = 0*nNormalAcceleration;
					iMaxSpeed = 3*iNormalMaxSpeed;
					
					//如果持续时间超过最长持续时间，则进入强制冷却状态
					if( iLastFrame >= iColdDownFrames )
					{
						iLastFrame = iColdDownFrames;
						iState = STATE_WIND_CD;
					}
						
					break;
				}
				case STATE_READY:
				{
					//处于就绪状态
					MoveCtrlKeyboard();
					Friction(nFrictionRate);
					test.gotoAndStop("IDLE");
					iMass = iNormalMass;//质量恢复到正常水平
					nAcceleration = nNormalAcceleration;
					iMaxSpeed = iNormalMaxSpeed;
					
					if( iLastFrame >= 0 )
						iLastFrame -= 2;//以两倍的速度冷却
					else
						iLastFrame = 0;
					
					break;
				}
				case STATE_WIND_CD:
				{
					//处于旋风强制冷却状态
					MoveCtrlKeyboard();
					Friction(nFrictionRate);
					test.gotoAndStop("IDLE");
					iMass = iNormalMass;//质量恢复到正常水平
					nAcceleration = nNormalAcceleration;
					iMaxSpeed = iNormalMaxSpeed;
					
					//以一倍速度冷却，冷却结束后进入就绪状态
					if( iLastFrame >= 0 )
						iLastFrame -=1;
					else
					{
						iLastFrame = 0;
						iState = STATE_READY;
					}
						
					break;
				}
				case STATE_FREEZED:
				{
					//撞击输了就调用硬直函数与摩擦函数
					Friction(nFrictionRate);
					iMass = iNormalMass;//质量恢复到正常水平
					nAcceleration = nNormalAcceleration;
					iMaxSpeed = iNormalMaxSpeed;
					
					//以一倍速度冷却旋风技能
					if( iLastFrame >= 0 )
						iLastFrame -= 1;
					else
						iLastFrame = 0;
					
					UpdateFreezingState();
					break;
				}
				case STATE_DEAD:
				{
					//播放死亡的动画，动画结束后跳转到结束状态
					Friction(nFrictionRate);
					if( test.currentFrame == test.totalFrames )
					{
						iState = STATE_END;
					}
					break;
				}
				case STATE_END:
				{
					break;
				}
			}
		}
		
		public function Dead():void
		{
			iState = STATE_DEAD;
			test.gotoAndPlay("DEAD");
		}
		public function Pause():void
		{
			
		}
		
		public function Continue():void
		{
			
		}
		
		public function Clear():void
		{
			stage.removeEventListener(MouseEvent.MOUSE_DOWN,StartWhirlwind);
			stage.removeEventListener(MouseEvent.MOUSE_UP,EndWhirlwind);
		}
		
	
	}
}